package jsyf.oa.web.system;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.system.RightFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Administrator on 2017/12/7 0007.
 */
@Api(value = "菜单接口", description = "菜单接口")
@Controller
@RequestMapping("/right")
public class RightController {
    @Autowired
    private RightFacade rightFacade;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String result = rightFacade.save(jsonData);
        if (!StringUtils.isEmpty(result)) {
            JSONObject jsonResult = new JSONObject();
            jsonResult.put("id", result);
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonResult));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("保存失败！"));
        }
    }
    @RequestMapping(value = "/sort.json", method = RequestMethod.POST)
    public void sort(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        Boolean result = rightFacade.sort(jsonParam.getString("rightIds"));
        JSONObject objResult = new JSONObject();
        objResult.put("success", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }
    @RequestMapping(value = "/getById.json", method = RequestMethod.POST)
    public void getById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        JSONObject result = rightFacade.getById(jsonParam.getString("id"));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
    }

    @RequestMapping(value = "/removeById.json", method = RequestMethod.POST)
    public void delById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        boolean result = rightFacade.removeById(jsonParam.getString("id"));
        if (result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("删除失败！"));
        }
    }

    @RequestMapping(value = "/getTreeNodes.json", method = RequestMethod.POST)
    public void getTreeNodes(HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> result = rightFacade.getTreeNodes();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }

    @RequestMapping(value = "/getMenuByUserId.json", method = RequestMethod.POST)
    public void getMenuByUserId(HttpServletResponse response, @RequestBody String jsonData) {

        List<JSONObject> result = rightFacade.getMenuByUserId(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }

    @RequestMapping(value = "/getMenuByApp.json", method = RequestMethod.POST)
    public void getMenuByApp(HttpServletResponse response, @RequestBody String jsonData) {

        List<JSONObject> result = rightFacade.getMenuByApp(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }

    @RequestMapping(value = "/getRoleRightByUserId.json", method = RequestMethod.POST)
    public void getRoleRightByUserId(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        List<JSONObject> result = rightFacade.getRoleRightByUserId(jsonParam.getString("userId"));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }
}
